<?php
namespace app\tactics\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\tactics\model\Attr as AttrModel;
use think\Db;

/**
 * 销售策略
 */
class Price extends Admin
{
  //价格策略
  public function index(){

      $data = Db::name('admin_config')->paginate();

      return    ZBuilder::make('table')
                -> addTopButton('add')
                -> addTopButton('enable')
                -> addTopButton('disable')
                -> addTopButton('delete')
                -> addRightButtons(['edit', 'delete'])
                -> setSearch(array('name'=>'策略名称','id'=>'策略编号'))
                -> addOrder('id,name')
                -> addFilter('id,name')
                -> addTimeFilter('create_time')
                -> addColumn('id', '策略编号')
                -> addColumn('name', '策略名称','','','','fix-table-td')
                -> addColumn('value', '策略内容','','','','fix-table-td')
                -> addColumn('tips', '策略介绍','','','','fix-table-td')
                -> addColumn('create_time', '生效时间','datetime')
                -> addColumn('update_time', '失效时间','datetime')
                -> addColumn('status', '状态','switch')
                -> addColumn('right_button', '操作','btn')
                -> setRowList($data)
                -> fetch();
  }

  //新增价格策略
  public function add($record = []){
      //判断当前步骤
      if ($this->request->isPost()) {
          // 表单数据
          $data = $this->request->post();
          if(empty($data)) $this->error("未选择任何属性");
          //批量配置表单参数
          $params = array();

          foreach ($data as $key => $value) {
              for ($i=0; $i < sizeof($value); $i++) {
                  $map['attrcode'] = $value[$i];
                  $map['attrgroup'] = $key;
                  $item = AttrModel::where($map)->find();
                  switch ($item['inittype']) {
                      case 'Text':
                          $params[] = ['Text',$key.'_'.$item['attrcode'],$item['attrname']];
                          break;
                      case 'Select':
                          //设置下拉菜单
                          $table = $item['sourcetable'];
                          $fields = $item['datafield'];
                          $list = Db::name($table)->where($item['serchparams'])->field($fields)->select();
                          $ex = explode(',',$fields);
                          $listdata = array();
                          for ($c=0; $c < sizeof($list); $c++) {
                              $listdata[$list[$c][$ex[0]]] = $list[$c][$ex[1]];
                          }
                          $params[] = ['Select',$key.'_'.$item['attrcode'],$item['attrname'],'',$listdata];
                          break;
                      case 'SectionInput':
                          $params[] = ['Range',$key.'_'.$item['attrcode'],$item['attrname'],'','',['double' => 'true','grid' => 'true', 'min' => 0, 'max' => 1000, 'step' => 50]];
                          break;
					  case 'Linkages':
						  $params[] = ['Linkages',$key.'_'.$item['attrcode'],$item['attrname'],'',$item['sourcetable'],3];
							  break;	  
                      default:
                          break;
                  }
              }
          }
          $layout = array();
          //设置Layout参数
          foreach ($params as $key => $value) {
              'Range' == $value[0]  ?  $layout[$value[1]] = 12:$layout[$value[1]] = 3;
          }

		  $PriceLevel = Db::name('member_level')->field('id,name')->select();
		  $ex = explode(',',"id,name");
          $listdata = array();
          for ($c=0; $c < sizeof($PriceLevel); $c++) {
			$listdata[$PriceLevel[$c][$ex[0]]] = $PriceLevel[$c][$ex[1]];
          }	  
          return ZBuilder::make('form')
                    ->setPageTitle('【Step2】设置条件规则')
					->setPageTips('Tips:<br/>所有条件为AND关系，只有同时满足所有条件时才会触发该策略。')
                    ->addFormItems($params)
					->addFormItem('Radio','TargetNode','应用节点','',['BeforeAddToCart'=>'加入购物车','BeforeOrderSubmit'=>'提交订单之前','AfterSubmitOrder'=>'订单生成之后'])  //设置策略应用节点
					->addFormItem('select', 'TacticsOption', '执行操作','商品加入购物车之前只能修改商品价格和商品属性,订单提交之前可以重新设置商品价格以及部分订单参数;订单提交之后可以修改订单上的主数据不能修改明细行参数',['UpdatePrice'=>'商品执行价格','UpdateGoodsTags'=>'修改商品标签','UpdateOrderTages'=>'修改订单标签','UpdateOrder'=>'修改订单属性'],'','','form-group col-md-3')  //执行操作
					->addFormItem('select', 'NewPrice', '执行价格','请选择执行等级',$listdata,'','','form-group col-md-3')  //生效时间
					->addFormItem('daterange', 'Period', '生效时间')  //生效时间
					->addFormItem('textarea', 'Remark', '策略说明')  //策略说明
					->setTrigger('TacticsOption','UpdatePrice','NewPrice')
                    ->isAjax(false)
                    ->layout($layout)
                    ->fetch();
      }else {
          return    ZBuilder::make('form')
                    ->setPageTitle('【Step1】选择备选条件')
                    ->addCheckbox('Member', '客户相关', '', ['Grade' => '客户等级','Adr' => '客户区域','Group' => '客户分组','Type' => '客户行业'])
                    ->addCheckbox('Goods', '商品相关', '', ['Brand' => '商品品牌', 'Class' => '商品款号', 'Color' => '商品颜色','Size' => '商品尺码','Tags' => '商品标签','BuyNum' => '购买数量','Unit' => '商品单位'])
                    ->addCheckbox('Order', '订单相关', '', ['Warehouse' => '发货仓库', 'SendTo' => '收货区域', 'Total' => '订单金额', 'Invoice' => '发票相关','Logistic' => '物流方式'])
                    ->isAjax(false)
                    ->fetch();
      }

  }

  //启用价格策略
  public function enable($record = []){

  }

  //禁用价格策略
  public function disable($record = []){

  }

  //删除价格策略
  public function delete($record = []){

  }
  //编辑价格策略
  public function edit($record = []){
      dump($_GET);
  }

}
